package cn.lrwjz.androidguide.ui.exmple;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.appbar.MaterialToolbar;

import cn.lrwjz.androidguide.R;
import cn.lrwjz.androidguide.ui.exmple.fragment.DynamicFragment;
import cn.lrwjz.androidguide.ui.exmple.fragment.StaticFragment;

public class FragmentActivity extends AppCompatActivity implements DynamicFragment.OnFragmentInteractionListener {
    private StaticFragment staticFragment;
    private DynamicFragment dynamicFragment1;
    private DynamicFragment dynamicFragment2;

    public StaticFragment getStaticFragment() {
        return staticFragment;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);

        // 设置工具栏
        MaterialToolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("Fragment 示例");
        }

        // 获取静态Fragment实例
        staticFragment = (StaticFragment) getSupportFragmentManager()
                .findFragmentById(R.id.fragment_static);

        // 创建动态Fragment实例
        dynamicFragment1 = DynamicFragment.newInstance("这是动态Fragment 1");
        dynamicFragment2 = DynamicFragment.newInstance("这是动态Fragment 2");

        // 设置按钮点击事件
        Button btnAddFragment = findViewById(R.id.btn_add_fragment);
        btnAddFragment.setOnClickListener(v -> addFragment());

        Button btnReplaceFragment = findViewById(R.id.btn_replace_fragment);
        btnReplaceFragment.setOnClickListener(v -> replaceFragment());

        Button btnRemoveFragment = findViewById(R.id.btn_remove_fragment);
        btnRemoveFragment.setOnClickListener(v -> removeFragment());

        Button btnHideShowFragment = findViewById(R.id.btn_hide_show_fragment);
        btnHideShowFragment.setOnClickListener(v -> hideShowFragment());

        Button btnSendToStatic = findViewById(R.id.btn_send_to_static);
        btnSendToStatic.setOnClickListener(v -> sendMessageToStaticFragment());

        Button btnSendToDynamic = findViewById(R.id.btn_send_to_dynamic);
        btnSendToDynamic.setOnClickListener(v -> sendMessageToDynamicFragment());
    }

    // 添加Fragment
    private void addFragment() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (!dynamicFragment1.isAdded()) {
            transaction.add(R.id.fragment_container, dynamicFragment1);
//            transaction.addToBackStack(null);
            transaction.commit();
        }
    }

    // 替换Fragment
    private void replaceFragment() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, dynamicFragment2);
        // 放入回退栈
        transaction.addToBackStack("dynamicFragment2");
        transaction.commit();
    }

    // 移除Fragment
    private void removeFragment() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        Fragment fragment = fragmentManager.findFragmentById(R.id.fragment_container);
        if (fragment != null) {
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.remove(fragment);
            transaction.commit();
        }
    }

    // 显示/隐藏Fragment
    private void hideShowFragment() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (dynamicFragment1.isAdded() && dynamicFragment1.isVisible()) {
            transaction.hide(dynamicFragment1);
        } else if (dynamicFragment1.isAdded()) {
            transaction.show(dynamicFragment1);
        }
        transaction.commit();
    }

    // 向静态Fragment发送消息
    private void sendMessageToStaticFragment() {
        if (staticFragment != null) {
            staticFragment.updateContent("来自Activity的消息");
        }
    }

    // 向动态Fragment发送消息
    private void sendMessageToDynamicFragment() {
        if (dynamicFragment1.isAdded()) {
            // 通过Bundle传递数据
            Bundle bundle = new Bundle();
            bundle.putString(DynamicFragment.ARG_CONTENT, "来自Activity的消息");
            dynamicFragment1.setArguments(bundle);
            // 更新控件
            dynamicFragment1.updateContent();
        } else {
            Toast.makeText(this, "请先添加Fragment", Toast.LENGTH_SHORT).show();
        }
    }

    // 接收来自Fragment的消息
    @Override
    public void onFragmentInteraction(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 